package com.cms.recursoshumanos.main;

import net.rim.device.api.applicationcontrol.ApplicationPermissions;
import net.rim.device.api.applicationcontrol.ApplicationPermissionsManager;

/**
 * Requests the necessary permissions for the application.
 * This code was contributed by LifeMarks.
 * @author Matt York (matt@lifemarks.mobi)
 */
final class AppPermissionsManager {

  private static final ApplicationPermissionsManager apm = ApplicationPermissionsManager.getInstance();

  private AppPermissionsManager() {
  }

  /**
   * Requests the required application permissions. Currently the required permissions are
   * event injection (sending system level key strokes to other running applications) and
   * accessing files (accessing the file when a qrcode image is saved to the file system).
   */
  static void setPermissions() {
    setPermission(ApplicationPermissions.PERMISSION_INPUT_SIMULATION);
    setPermission(ApplicationPermissions.PERMISSION_FILE_API);
    setPermission(ApplicationPermissions.PERMISSION_INTERNET);
    setPermission(ApplicationPermissions.PERMISSION_WIFI);
    setPermission(ApplicationPermissions.PERMISSION_LOCATION_DATA);
    setPermission(ApplicationPermissions.PERMISSION_SERVER_NETWORK);
  }

  private static boolean setPermission(int permission) {
    boolean updatedPermissions = false;
    ApplicationPermissions ap = apm.getApplicationPermissions();
    if (ap.containsPermissionKey(permission)) {
      int eventInjectorPermission = ap.getPermission(permission);
      if (eventInjectorPermission != ApplicationPermissions.VALUE_ALLOW) {
        ap.addPermission(permission);
        updatedPermissions = apm.invokePermissionsRequest(ap);
      }
    } else {
      ap.addPermission(permission);
      updatedPermissions = apm.invokePermissionsRequest(ap);
    }
    return updatedPermissions;
  }

}

